#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import json
from collections import defaultdict
from pytools import mysqltool


summary_table_name = 'summary'
city_table_name = 'city'
cate_table_name = 'cate'


def get_dict_from_query(q):
    # print 'get_dict_from_query called'
    conn = mysqltool.connect_to_58_stats()
    xxx = mysqltool.get_a_dict(conn, q)
    return xxx


def get_tablename_from_cate_and_city(cate, city):
    if not hasattr(get_tablename_from_cate_and_city, 'cate2tablename'):
        q = 'select cate_id, table_prefix from display_cate left join dbwww58com_base_dicts.channel_define on display_cate.channel_id = channel_define.channel_id'
        get_tablename_from_cate_and_city.cate2tablename = get_dict_from_query(q)

    if cate == '0' and city == '0':
        return  summary_table_name
    if cate == '0':
        return city_table_name
    if city == '0':
        return cate_table_name
    return get_tablename_from_cate_and_city.cate2tablename.get(cate, None)


def get_catename_from_cateid(cateid):
    if not hasattr(get_catename_from_cateid, 'id2name'):
        q = 'select cate_id, cate1, cate2, cate3, cate4, cate5 from display_cate'
        get_catename_from_cateid.id2name = get_dict_from_query(q)

    return get_catename_from_cateid.id2name.get(cateid, None)


def get_cityname_from_cityid(cityid):
    if not hasattr(get_cityname_from_cityid, 'id2name'):
        q = 'select local_id, city1, city2, city3, city4 from display_local'
        get_cityname_from_cityid.id2name = get_dict_from_query(q)

    return get_cityname_from_cityid.id2name.get(cityid, None)


def append_cate_and_city_info_to_data(filename):
    tabledict = defaultdict(list)

    for record in open(filename, 'rb').readlines():
        record = record.strip()
        day, cate, city, other = record.split(',', 3)

        tablename = get_tablename_from_cate_and_city(cate, city)
        if tablename is None:
            print 'error on get tablename:', record
            continue

        if tablename == summary_table_name:
            txt = ','.join([day, other])
            tabledict[tablename].append(txt)
            continue

        if tablename == city_table_name:
            append = get_cityname_from_cityid(city)
            if append is not None:
                txt = ','.join([day, city] + append + [other])
                tabledict[tablename].append(txt)
            continue

        if tablename == cate_table_name:
            append = get_catename_from_cateid(cate)
            if append is not None:
                txt = ','.join([day, cate] + append + [other])
                tabledict[tablename].append(txt)
            continue

        try:
            append = get_catename_from_cateid(cate) + get_cityname_from_cityid(city)
        except:
            append = None
        if append is None:
             print 'error on get cityname, or catename:', record
             continue
        txt = ','.join([day, cate, city] + append + [other])
        tabledict[tablename].append(txt)

    return dict(tabledict)


def dump_data_to_mysql(tabledict, whichtable,month, day):
    rootdir = '/home/resys/hbasedata/data'
    for k, v in tabledict.items():
        tablename = [k, whichtable]
        if k != summary_table_name:
            tablename.append(month)
        tablename = '_'.join(tablename)

        newfile = os.path.join(rootdir, k + '-' + whichtable)
        with open(newfile, 'wb') as f:
            f.write('\n'.join(v))
        
        q = "delete form %s where stat_date=%s;"%(tablename,day)
        cmdtxt = 'echo \'%s\' | mysql --auto-rehash -h10.5.20.16 -P58885 -u"ec_stat" -p"L#c8UM5RGh5C" -D dbwww58com_58_stats' % (q)
        os.system(cmdtxt)
         
        loaddata = 'load data low_priority local infile "%s" into table %s fields terminated by ",";' % (newfile, tablename)
        # print loaddata
        cmdtxt = 'echo \'%s\' | mysql --auto-rehash -h10.5.20.16 -P58885 -u"ec_stat" -p"L#c8UM5RGh5C" -D dbwww58com_58_stats' % (loaddata)
        # print cmdtxt
        os.system(cmdtxt)
        os.system('rm ' + newfile)


if __name__ == '__main__':
    if len(sys.argv) != 5:
        print 'usage: %s tablename datafile month' % (sys.argv[0])
        sys.exit(1)

    # tablename = 'detail_analytics'
    # tablename = 'list_analytics'
    # dtest = 'data/hoho.txt'
    # day = '201301'
    tablename = sys.argv[1]
    dtest = sys.argv[2]
    month = sys.argv[3]
    day = sys.argv[4]
    
    #print day
    #return
    tabledict = append_cate_and_city_info_to_data(dtest)
    print tabledict.keys()
    dump_data_to_mysql(tabledict, tablename, month,day)

